package cn.nebula.music.service;

import java.sql.Date;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.nebula.music.dao.SongSonglistDAO;
import cn.nebula.music.dao.SonglistDAO;
import cn.nebula.music.dao.UserSonglistDAO;
import cn.nebula.music.domain.Song;
import cn.nebula.music.domain.SongSonglist;
import cn.nebula.music.domain.Songlist;
import cn.nebula.music.domain.User;
import cn.nebula.music.domain.UserSonglist;

@Service
public class SonglistServiceImpl implements SonglistService {

	@Autowired
	private SonglistDAO songlistDAO;

	@Autowired
	private SongSonglistDAO songSonglistDAO;

	@Autowired
	private UserSonglistDAO userSonglistDAO;

	@Override
	public Set<Songlist> findAllSonglists() throws Exception {
		return songlistDAO.findAllSonglists();
	}

	@Override
	public Set<Songlist> findAllSonglistsOfBT() throws Exception {
		return songlistDAO.findAllSonglistsOfBT();
	}

	@Override
	public Songlist findSonglistById(Integer id) throws Exception {
		return songlistDAO.findSonglistById(id);
	}

	@Override
	public Set<Songlist> findSonglistsByName(String name) throws Exception {
		return songlistDAO.findSonglistsByName(name);
	}

	@Override
	public Set<Songlist> findSonglistsByUpdatetime(Date updatetime) throws Exception {
		return songlistDAO.findSonglistsByUpdatetime(updatetime);
	}

	@Override
	public Set<Songlist> findSonglistSByUser(Integer userId) throws Exception {
		return songlistDAO.findSonglistSByUser(userId);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void addSonglist(Songlist songlist) throws Exception {
		songlistDAO.addSonglist(songlist);
		Set<Song> songs = songlist.getSongs();
		if (songs != null) {
			for (Song song : songs) {
				SongSonglist ss = new SongSonglist();
				ss.setSonglist(songlist.getId());
				ss.setSong(song.getId());
				songSonglistDAO.addSongSonglist(ss);
			}
		}
		Set<User> users = songlist.getUsers();
		if (users != null) {
			for (User user1 : users) {
				UserSonglist us = new UserSonglist();
				us.setSonglist(songlist.getId());
				us.setUser(user1.getId());
				userSonglistDAO.addUserSonglist(us);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void updateSonglist(Songlist songlist) throws Exception {
		songlistDAO.updateSonglist(songlist);
		Boolean isModify = false;
		int size1 = 0;// 更改前的数量
		int size2 = 0;// 更改后的数量
		Set<User> users1 = findSonglistById(songlist.getId()).getUsers();
		Set<User> users2 = songlist.getUsers();
		if (users1 != null) {
			size1 = users1.size();
		}
		if (users2 != null) {
			size2 = users2.size();
		}
		if (size1 == size2) {
			if (!users1.toString().equals(users2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (User user : users1) {
				UserSonglist us = new UserSonglist();
				us.setSonglist(songlist.getId());
				us.setUser(user.getId());
				userSonglistDAO.deleteUserSonglist(us);
			}
			for (User user : users2) {
				UserSonglist us = new UserSonglist();
				us.setSonglist(songlist.getId());
				us.setUser(user.getId());
				userSonglistDAO.addUserSonglist(us);
			}
		}
		isModify = false;
		size1 = 0;// 更改前的数量
		size2 = 0;// 更改后的数量
		Set<Song> songs1 = findSonglistById(songlist.getId()).getSongs();
		System.out.println(songlist.getSongs());
		Set<Song> songs2 = songlist.getSongs();
		if (songs1 != null) {
			size1 = songs1.size();
		}
		if (songs2 != null) {
			size2 = songs2.size();
		}
		if (size1 == size2) {
			if (!songs1.toString().equals(songs2.toString())) {
				isModify = true;
			}
		} else {
			isModify = true;
		}
		if (isModify == true) {
			for (Song song : songs1) {
				SongSonglist ss = new SongSonglist();
				ss.setSonglist(songlist.getId());
				ss.setSong(song.getId());
				songSonglistDAO.deleteSongSonglist(ss);
			}
			for (Song song : songs2) {
				SongSonglist ss = new SongSonglist();
				ss.setSonglist(songlist.getId());
				ss.setSong(song.getId());
				songSonglistDAO.addSongSonglist(ss);
			}
		}
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public void deleteSonglistById(Integer id) throws Exception {
		songSonglistDAO.deleteSongSonglistBySonglistId(id);
		userSonglistDAO.deleteUserSonglistBySonglistId(id);
		songlistDAO.deleteSonglistById(id);
	}

}
